package com.reggie.module.controller;

import com.reggie.common.ResultResponseVo;
import com.reggie.module.entity.dto.categoryDto.AddCategoryDto;
import com.reggie.module.entity.dto.categoryDto.UpdateCategoryDto;
import com.reggie.module.entity.po.Category;
import com.reggie.module.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * @Author 早睡蛋
 * @Date 2022/9/17 14:30:42
 * @Desc:
 */
@RestController
@Slf4j
@RequestMapping("/category")
@CrossOrigin
@Validated
@Api(tags = "菜单分类模块")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 菜单分类分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜单分类分页查询")
    public ResultResponseVo page(int page, int pageSize) {
        return categoryService.selectByPage(page, pageSize);
    }

    /**
     * 新增菜单分类
     *
     * @param addCategoryDto
     * @param request
     * @return
     */
    @PostMapping
    public ResultResponseVo save(@RequestBody @Valid AddCategoryDto addCategoryDto, HttpServletRequest request) {
        return categoryService.add(addCategoryDto, request);
    }

    /**
     * 删除分类
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public ResultResponseVo delete(Long ids) {
        log.info("要删除的菜单分类id为：{}", ids);
        return categoryService.deleteById(ids);
    }

    /**
     * 文件下载（回显）
     * @param updateCategoryDto
     * @param request
     * @return
     */
    @PutMapping
    public ResultResponseVo update(@RequestBody @Valid UpdateCategoryDto updateCategoryDto, HttpServletRequest request) {
        return categoryService.updateCategory(updateCategoryDto, request);
    }

    /**
     * 获取菜品分类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public ResultResponseVo getList(Category category){
        return categoryService.listByType(category);

    }


}
